package com.chen.service.Impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.common.Result;
import com.chen.mapper.NewsMapper;
import com.chen.pojo.News;
import com.chen.pojo.User;
import com.chen.service.NewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service("newsServiceImpl")
@SuppressWarnings("all")
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
    @Resource
    private NewsMapper newsMapper;

    /**
     * 撰写博客
     *
     * @param news
     * @return
     */
    public Result<?> addNews(News news) {
        news.setTime(new Date());
        if (StrUtil.isBlank(news.getImg())){
            news.setImg("https://www.static.talkxj.com/articles/885607723eee90de876c1dadea3cced1.jpg");
        }
        return Result.success( newsMapper.insert(news));
    }


    /**
     * 编辑博客
     *
     * @param news
     * @return
     */
    public Result<?> updateNews(News news) {

        return Result.success(newsMapper.updateById(news));
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public Result<?> getById(Long id) {
        return Result.success(newsMapper.selectById(id));
    }

    /**
     * 查询
     * @param id
     * @return
     */
    public Result<?> delete( Long id) {
        newsMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 分页
     * @param pageNum
     * @param pageSize
     * @param search
     * @return
     */
    public Result<?> findPage(Integer pageNum,  //页号
                              Integer pageSize, //一页展示的数量
                              String search) {

//        模糊查询
        LambdaQueryWrapper<News> warapper = Wrappers.<News>lambdaQuery().like(News::getTitle, search);
        if (StrUtil.isBlank(search)) {
            warapper.like(News::getTitle, search);
    }
        Page<News> userPage = newsMapper.selectPage(new Page<>(pageNum, pageSize), warapper);
        return Result.success(userPage);

    }

    @Override
    public List<News> finfType() {
       return newsMapper.fingType();

    }

}
